package com.fs.service;

import com.fs.entity.Door;
import com.fs.entity.Room;
import com.fs.repository.DoorRepository;
import com.fs.service.bo.device.BoSwitch;
import com.fs.service.bo.room.BoRoom;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional
public class DoorService {

    private final DoorRepository doorDao;

    public DoorService(DoorRepository doorDao) {
        this.doorDao = doorDao;
    }

    @Cacheable(cacheNames = "DoorService#findRooms")
    public List<BoRoom> findRooms(int doorId) {
        Optional<Door> optDoor = doorDao.findById(doorId);
        if (!optDoor.isPresent()) {
            throw new NotFoundException("没有找到门");
        }

        List<Room> rooms = optDoor.get().getRooms();
        return rooms.stream().map(BoRoom::new).collect(Collectors.toList());
    }

}
